package com.slipper.system.codeless.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.AppVersionDao;
import com.slipper.system.codeless.bo.AppVersionBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class AppVersionService {
    @Resource
    private AppVersionDao appVersionDao;
    
    public AppVersionBO queryBy(String id) {
        return appVersionDao.queryBy(id);
    }
    
    public List<AppVersionBO> queryList(AppVersionBO bo) {
        return appVersionDao.queryList(bo);
    }
    
    public PageInfo<AppVersionBO> queryPage(AppVersionBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(appVersionDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(AppVersionBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return appVersionDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return appVersionDao.insert(bo);
        }
    }
    
    @WriteTransactional
    public int delete(AppVersionBO bo) {
        return appVersionDao.delete(bo);
    }

    public AppVersionBO queryLatestBy(String code, String type) {
        List<AppVersionBO> versionList = appVersionDao.queryLatestBy(code, type);
        return versionList.isEmpty() ? null : versionList.getFirst();
    }
}
